package com.mentalhealth.diary.config;

import com.mentalhealth.diary.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置类
 * 注册和配置拦截器
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册JWT拦截器
        registry.addInterceptor(jwtInterceptor)
                // 添加拦截路径，所有/api路径下的请求都需要验证（除了登录和注册）
                .addPathPatterns("/api/**")
                // 排除不需要验证的路径
                .excludePathPatterns("/api/auth/login")
                .excludePathPatterns("/api/auth/register")
                .excludePathPatterns("/api/auth/send-code")
                // 排除静态资源
                .excludePathPatterns("/swagger-ui/**", "/v3/api-docs/**")
                .excludePathPatterns("/uploads/**");
    }
}